package main

import (
	"fmt"
	"strings"
	"io"
	"bufio"
)

/**
 * @description 
 * @time 2018/5/6 17:11
 * @version 
 */

type intGen func() int


// 1, 1, 2, 3, 5, 8, 13, ...
func Fibonacci() func() int {
	a, b := 0, 1
	return func() int {
		a, b = b, a+b
		return a
	}
}

func FibonacciFunc() intGen {
	a, b := 0, 1
	return func() int {
		a, b = b, a+b
		return a
	}
}

func (g intGen) Read(p []byte) (n int, err error) {
	next := g()
	//因为intGen函数不是常规的输入流，每次调用都会生成新的数据，
	// 所以Read会一直被调用,这里需要做一个判断来退出读取
	if next > 10000 {
		return 0, io.EOF
	}
	s := fmt.Sprintf("%d\n", next)
	//将next写入到p
	return strings.NewReader(s).Read(p)
}

//5-5 系统常用接口,6:00 解读
func printFileContents(reader io.Reader) {
	scanner := bufio.NewScanner(reader)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

func main() {
	f := FibonacciFunc()
	printFileContents(f)

}
